/*
//
//              INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license  agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in  accordance  with the terms of that agreement.
//    Copyright (c) 2007-2008 Intel Corporation. All Rights Reserved.
//
//
*/

#include "umc_defs.h"
#if defined(UMC_ENABLE_AVS_VIDEO_ENCODER)

#include "umc_avs_enc_header_init.h"
#include "umc_avs_sequence_header.h"
#include "umc_avs_picture_header.h"
#include "umc_video_data.h"
#include "umc_avs_enc.h"
#include "umc_avs_consts.h"

namespace UMC
{

Status InitializeSequenceHeader(AVS_SEQUENCE_HEADER *pSeqHeader, AVSVideoEncoderParams *pParams)
{
    // check error(s)
    if ((NULL == pSeqHeader) ||
        (NULL == pParams))
        return UMC_ERR_NULL_PTR;

    // set sequence header start code
    pSeqHeader->video_sequence_start_code = VIDEO_SEQUENCE_START_CODE;

    // set sequence profile
    if (AVS_PROFILE_JIZHUN != pParams->profile)
    {
        return UMC_ERR_INVALID_PARAMS;
    }
    pSeqHeader->profile_id = pParams->profile;

    // set sequence level
    if ((AVS_LEVEL_20 != pParams->level) &&
        (AVS_LEVEL_40 != pParams->level) &&
        (AVS_LEVEL_42 != pParams->level) &&
        (AVS_LEVEL_60 != pParams->level) &&
        (AVS_LEVEL_62 != pParams->level))
    {
        return UMC_ERR_INVALID_PARAMS;
    }
    pSeqHeader->level_id = pParams->level;

    // set frames parameters
    pSeqHeader->progressive_sequence = (pParams->info.interlace_type == PROGRESSIVE) ? (1) : (0);
    if (0 > pParams->info.clip_info.width)
    {
        return UMC_ERR_INVALID_PARAMS;
    }
    pSeqHeader->horizontal_size = pParams->info.clip_info.width;
    if (0 > pParams->info.clip_info.height)
    {
        return UMC_ERR_INVALID_PARAMS;
    }
    pSeqHeader->vertical_size = pParams->info.clip_info.height;
    if ((YUV420 == pParams->info.color_format) ||
        (YV12 == pParams->info.color_format))
    {
        pSeqHeader->chroma_format = AVS_CHROMA_420_FORMAT;
    }
    else if (YUV422 == pParams->info.color_format)
    {
        pSeqHeader->chroma_format = AVS_CHROMA_422_FORMAT;
    }
    else
    {
        return UMC_ERR_INVALID_PARAMS;
    }
    pSeqHeader->sample_precission = 1;
    pSeqHeader->aspect_ratio = 1;
    pSeqHeader->frame_rate = pParams->info.framerate;

    // set bit rate parameters
    pSeqHeader->bit_rate_lower = (pParams->info.bitrate / 400) & 0x03ffff;
    pSeqHeader->bit_rate_upper = (pParams->info.bitrate / 400) >> 18;
    pSeqHeader->low_delay = (0 == pParams->m_maxBLength) ? (1) : (0);
    pSeqHeader->bbv_buffer_size = (Ipp32u) -1;

    return UMC_OK;

} // Status InitializeSequenceHeader(AVS_SEQUENCE_HEADER *pSeqHeader, VideoData *pVideoSrc)

Status InitializeIPictureHeader(AVS_PICTURE_HEADER *pPicHeader, VideoData *pVideoSrc)
{
    AVS_PICTURE_HEADER picHeader;

    // check error(s)
    if ((NULL == pPicHeader) ||
        (NULL == pVideoSrc))
        return UMC_ERR_NULL_PTR;

    // copy initial parameters
    picHeader = *pPicHeader;

    // set picture parameters
    picHeader.PictureType = AVS_I_PICTURE;
    picHeader.picture_start_code = I_PICTURE_START_CODE;
    picHeader.picture_distance = (picHeader.picture_distance + 1) % 256;
    picHeader.progressive_frame = 1;
    picHeader.picture_structure = 1;
    picHeader.fixed_picture_qp = 1/*1*/;
    picHeader.picture_qp = 12;
    picHeader.skip_mode_flag = 0;
    picHeader.loop_filter_disable = 1;
    picHeader.picture_reference_flag = 1;

    // copy parameters back
    *pPicHeader = picHeader;

    return UMC_OK;

} // Status InitializeIPictureHeader(AVS_PICTURE_HEADER *pPicHeader, VideoData *pVideoSrc)

Status InitializePPictureHeader(AVS_PICTURE_HEADER *pPicHeader, VideoData *pVideoSrc)
{
    AVS_PICTURE_HEADER picHeader;

    // check error(s)
    if ((NULL == pPicHeader) ||
        (NULL == pVideoSrc))
        return UMC_ERR_NULL_PTR;

    // copy initial parameters
    picHeader = *pPicHeader;

    // set picture parameters
    picHeader.PictureType = AVS_P_PICTURE;
    picHeader.picture_start_code = BP_PICTURE_START_CODE;
    picHeader.picture_coding_type = 0x01; // P frame!!!
    picHeader.picture_distance = (picHeader.picture_distance + 1) % 256;
    picHeader.progressive_frame = 1;
    picHeader.picture_structure = 1;
    picHeader.fixed_picture_qp = 1; // 1 means that qp doen't change in the picture
    picHeader.picture_qp = 12;
    //"1" indicates that the macroblock skip mode uses run-length coding.
    //"0" indicates the macroblock skip mode is derived from mb_type.
    picHeader.skip_mode_flag = 0;
    picHeader.loop_filter_disable = 1;
    picHeader.picture_reference_flag = 1;

    // copy parameters back
    *pPicHeader = picHeader;

    return UMC_OK;

} // Status InitializePPictureHeader(AVS_PICTURE_HEADER *pPicHeader, VideoData *pVideoSrc)

Status InitializeBPictureHeader(AVS_PICTURE_HEADER *pPicHeader, VideoData *pVideoSrc)
{
    AVS_PICTURE_HEADER picHeader;

    // check error(s)
    if ((NULL == pPicHeader) ||
        (NULL == pVideoSrc))
        return UMC_ERR_NULL_PTR;

    // copy initial parameters
    picHeader = *pPicHeader;

    // set picture parameters
    picHeader.PictureType = AVS_B_PICTURE;
    picHeader.picture_start_code = BP_PICTURE_START_CODE;
    picHeader.picture_coding_type = 0x10; // B frame!!!
    picHeader.picture_distance = (picHeader.picture_distance + 1) % 256;
    picHeader.progressive_frame = 1;
    picHeader.picture_structure = 1;
    picHeader.fixed_picture_qp = 1; // 1 means that qp doen't change in the picture
    picHeader.picture_qp = 12;
    //"1" indicates that the macroblock skip mode uses run-length coding.
    //"0" indicates the macroblock skip mode is derived from mb_type.
    picHeader.skip_mode_flag = 0;
    picHeader.loop_filter_disable = 1;
    picHeader.picture_reference_flag = 1;


    // copy parameters back
    *pPicHeader = picHeader;

    return UMC_OK;

} // Status InitializeBPictureHeader(AVS_PICTURE_HEADER *pPicHeader, VideoData *pVideoSrc)

} // namespace UMC

#endif // defined(UMC_ENABLE_AVS_VIDEO_ENCODER)
